package de.uni.muenster.idw2.location.bluetooth.service.transformations;

import android.util.Log;

/**
 * @author lrodriguez
 *
 */

public class SimilarityTransformer{
	
	/**
	 * The region code for Germany: <country telephone code>00
	 */
	private static final int REGION_GERMANY = 4900;
	
	double [] parameters;
	
    /**
     * @param parameters Contains the parameters for the transformation (i.e. a0, a, b0, b)
     */
    SimilarityTransformer(double []parameters) {
		this.parameters = parameters;
	}
	
	/**This method is for transforming coordinates from the source coordinate system to the 
	 * target coordinate system (i.e Gauss-Krueger to pixel coordinates). The parameters for the 
	 * transformation used in this transformation are the ones obtained by calling similarityAlgorithm. 
	 * @param xsource
	 * @param ysource
	 * @return point in the target coordinate system.
	 */
	public ResultPoint transformUsingSimilarity(double xsource, double ysource){
		return  Similarity.transformWithSimilarity(parameters, xsource, ysource);
	}
	
	/**
	 * Transforms pixle coordinates into WGS 84 coordinates.
	 * 
	 * @param wgs84xsourceLongitude longitude in wgs84
	 * @param wgs84ysourceLatitude latitude in wgs84
	 * @return Point in target reference system
	 */
	public ResultPoint transformUsingSimilarityFromWGS84(double wgs84xsourceLongitude, double wgs84ysourceLatitude){
		double xsource, ysource;
		GkPoint point = TransformCoordinates.wgs84ToGermanGk(wgs84ysourceLatitude, wgs84xsourceLongitude);
		xsource = point.getNorthing(); //latitude
		ysource = point.getRawEasting(); //long
		//given that previously should be called the method Similarity.similarityAlgorithm 
		//the field parameters should be filled. 
		return  Similarity.transformWithSimilarity(parameters, xsource, ysource);
	}
	
	/**
	 * Transforms WGS 84 coordinates (of Germany) into pixel coordinates.
	 * 
	 * This method is only applicable for Germany.
	 * 
	 * @param xsource longitude in wgs84
	 * @param ysource latitude in wgs84
	 * @return Point in WGS84 (x = longitude, y = latitude)
	 */
	public CWPoint transformUsingSimilarityToWGS84inGermany(double xsource, double ysource){
		
		//given that previously should be called the method Similarity.similarityAlgorithm 
		//the field parameters should be filled. 
		ResultPoint resultPointGk = Similarity.transformWithSimilarity(parameters, xsource, ysource);
		
		double xtargetNorthing, ytargetGkEasting;
		xtargetNorthing = resultPointGk.x;
		ytargetGkEasting = resultPointGk.y;
		
		GkPoint point = new GkPoint(ytargetGkEasting, xtargetNorthing, REGION_GERMANY);
		CWPoint cwPoint = TransformCoordinates.GkToWgs84(point, REGION_GERMANY);
		
		return cwPoint;
	}
	
	/* This is the test of the similarity transformation and also the example of how to 
	 * perform a similarity transformation.
	 * */
	public static void testSimilarity(){

		//base points in a projected coordinate system (i.e Gauss - Krueger)
		//similarity transformation requires two points in each system as a basis
		double xsource1 = 3405514.224;
		double ysource1 = 5760507.859;

		double xsource2 = 3403246.093;
		double ysource2 = 5757115.358;

		//base points on a target system (in this case is a pixel coordinate system) 
		double xtarget1 = 2607;
		double ytarget1 = 970;

		double xtarget2 = 1969;
		double ytarget2 = 2017;
		
		//First step (get a transformer) the transformer contains the parameters 
		//needed for further transformations
		SimilarityTransformer transformer = Similarity.similarityAlgorithm(xsource1, ysource1, xsource2, ysource2,
				  xtarget1, ytarget1, xtarget2, ytarget2);
		
		//Pick a point in the source
		double xsource4 = 3405658.944; 
		double ysource4 = 5758875.050; 
		
		//Perform the actual transformation
		ResultPoint resPoint = transformer.transformUsingSimilarity(xsource4, ysource4);
		Log.e("Test Similarity Result: ", resPoint.toString());

	}
	
	/* 
	 * */
	public static void testSimilarityWGS84(){

		//base points in a projected coordinate system (i.e Gauss - Krueger)
		//similarity transformation requires two points in each system as a basis
		// it is worth to note that x here is considered longitude and y latitude 
		double xsource1 = 7.606936;
		double ysource1 = 51.960102;

		double xsource2 = 7.60708;
		double ysource2 =   51.959838;

		//base points on a target system (in this case is a pixel coordinate system) 
		//this point are "unreal" pixel coordinates in an image.
		double xtarget1 = 2607;
		double ytarget1 = 970;
        //TODO: Eva would be good to test it with real values..The only real are the wg84 coordinates that 
		//are the dongles coordinates.
		double xtarget2 = 1969;
		double ytarget2 = 2017;
		
		//First step (get a transformer) the transformer contains the parameters 
		//needed for further transformations
		SimilarityTransformer transformer = Similarity.similarityAlgorithmFromWGS84(xsource1, ysource1, xsource2, ysource2,
				  xtarget1, ytarget1, xtarget2, ytarget2);
		
		//Pick a point in the source
		double xsource4 = 7.607171; 
		double ysource4 = 51.960062; 
		
		//Perform the actual transformation
		ResultPoint resPoint = transformer.transformUsingSimilarity(xsource4, ysource4);
		Log.e("Test Similarity Result: ", resPoint.toString());

	}
	
}
